type SafeArea = UniApp.SafeArea | undefined
type NumOrUndef = number | undefined
type WinInfo = { safeArea: SafeArea, screenWidth: number, screenHeight: number, windowWidth: number, windowHeight: number, windowTop: NumOrUndef, windowBottom: number, tabbarHeight: number, navigationHeight: number, statusBarHeight: NumOrUndef, getSystemInfo: UniApp.GetWindowInfoResult } | null
let winInfo: WinInfo = null
/**
 * 获取屏幕窗口安全高度和宽度
 * 注意是针对种屏幕的统一计算，统一高度，不再让uni获取有效高度而烦恼。
 * 请一定要在onMounted或者onLoad中调用，否则不准确在h5端。
 * @return {WinInfo} winInfo
 */
export function getWindow(reset?: boolean): WinInfo {
  if (reset) winInfo = null
  if (winInfo) return winInfo
  const getSystemInfo = uni.getWindowInfo()

  const tabbarHeight = 50
  const navigationHeight = 44
  const screenWidth = getSystemInfo.screenWidth
  const screenHeight = getSystemInfo.screenHeight

  const windowWidth = getSystemInfo.windowWidth
  const windowHeight = getSystemInfo.windowHeight
  const windowTop = getSystemInfo.windowTop
  const windowBottom = getSystemInfo.windowBottom

  const statusBarHeight = getSystemInfo.statusBarHeight

  winInfo = { safeArea: getSystemInfo.safeArea, screenWidth, screenHeight, windowWidth, windowHeight, tabbarHeight, navigationHeight, statusBarHeight, windowTop, windowBottom, getSystemInfo }
  return winInfo
}

type DeviceInfo = UniApp.GetDeviceInfoResult | null
/**
 * 获取屏幕窗口安全高度和宽度
 * 注意是针对种屏幕的统一计算，统一高度，不再让uni获取有效高度而烦恼。
 * 请一定要在onMounted或者onLoad中调用，否则不准确在h5端。
 * @return {DeviceInfo} deviceInfo 设备信息
 */
let deviceInfo: DeviceInfo = null
export function getDeviceInfo(reset?: boolean): DeviceInfo {
  if (reset) deviceInfo = null
  if (deviceInfo) return deviceInfo
  deviceInfo = uni.getDeviceInfo()
  return deviceInfo
}

/**
 * 获取设备信息
 *
 * @return {object}
 */
let systemInfo: UniApp.GetSystemInfoResult | null = null
export function getSystemInfo() {
  if (systemInfo) return systemInfo
  systemInfo = uni.getSystemInfoSync()
  return systemInfo
}

/**
 * 获取屏幕宽度
 *
 * @return {number}
 */
export const screenWidth = (): number => getSystemInfo().screenWidth
/**
 * 获取屏幕高度
 *
 * @return {number}
 */
export const screenHeight = (): number => getSystemInfo().screenHeight

/**
 * @description 微信小程序右上角按钮位置信息
 */
const menuButtonRect = {} as UniApp.GetMenuButtonBoundingClientRectRes
// #ifdef MP
const menuButton = uni.getMenuButtonBoundingClientRect()
menuButtonRect.top = menuButton.top
menuButtonRect.left = menuButton.left
menuButtonRect.width = menuButton.width
menuButtonRect.height = menuButton.height
// #endif
export { menuButton }
